package com.train.daily.mapper;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.train.common.resp.PageResp;
import com.train.daily.dto.ticket.DailyTrainTicketPageDTO;
import com.train.daily.dto.ticket.DailyTrainTicketQueryOneDTO;
import com.train.daily.po.DailyTrainTicketPO;
import com.train.daily.resp.ticket.DailyTrainTicketPageResp;
import com.train.daily.resp.ticket.DailyTrainTicketQueryOneResp;
import com.train.mybatis.base.MyBaseMapper;
import org.apache.ibatis.annotations.Param;

import java.time.LocalDate;
import java.util.List;

/**
 * @author baitiaojun
 * @description 针对表【dailyTrainTicket(余票信息)】的数据库操作Mapper
 * @createDate 2025-03-07 00:09:31
 * @Entity generator.domain.DailyTrainTicket
 */
public interface DailyTrainTicketMapper extends MyBaseMapper<DailyTrainTicketPO> {

    default PageResp<DailyTrainTicketPageResp> pageList(DailyTrainTicketPageDTO dailyTrainTicketPageDTO) {
        LambdaQueryWrapper<DailyTrainTicketPO> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ObjectUtil.isNotNull(dailyTrainTicketPageDTO.getDate()), DailyTrainTicketPO::getDate, dailyTrainTicketPageDTO.getDate())
                .like(StrUtil.isNotEmpty(dailyTrainTicketPageDTO.getCode()), DailyTrainTicketPO::getTrainCode, dailyTrainTicketPageDTO.getCode())
                .eq(StrUtil.isNotEmpty(dailyTrainTicketPageDTO.getStart()), DailyTrainTicketPO::getStart, dailyTrainTicketPageDTO.getStart())
                .eq(StrUtil.isNotEmpty(dailyTrainTicketPageDTO.getEnd()), DailyTrainTicketPO::getEnd, dailyTrainTicketPageDTO.getEnd())
                .orderBy(true, false, DailyTrainTicketPO::getDate)
                .orderBy(true, true, DailyTrainTicketPO::getTrainCode)
                .orderBy(true, true, DailyTrainTicketPO::getStartIndex)
                .orderBy(true, true, DailyTrainTicketPO::getEndIndex);
        PageResp<DailyTrainTicketPO> dailyTrainTicketPOPageResp = this.selectPage(dailyTrainTicketPageDTO, queryWrapper);
        List<DailyTrainTicketPageResp> dailyTrainTicketPageRespList = BeanUtil.copyToList(dailyTrainTicketPOPageResp.getData(), DailyTrainTicketPageResp.class);
        return new PageResp<>(dailyTrainTicketPOPageResp.getTotal(), dailyTrainTicketPageRespList);
    }

    default boolean deleteTrains(LocalDate date, List<String> trainCodes) {
        LambdaQueryWrapper<DailyTrainTicketPO> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(DailyTrainTicketPO::getDate, date);
        queryWrapper.in(DailyTrainTicketPO::getTrainCode, trainCodes);
        return this.delete(queryWrapper) > 0;
    }

    default boolean existsTrains(LocalDate date, List<String> trainCodes) {
        LambdaQueryWrapper<DailyTrainTicketPO> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(DailyTrainTicketPO::getDate, date);
        queryWrapper.in(DailyTrainTicketPO::getTrainCode, trainCodes);
        queryWrapper.last("limit 1");
        return this.exists(queryWrapper);
    }

    default DailyTrainTicketQueryOneResp getDailyTrainTicket(DailyTrainTicketQueryOneDTO dto) {
        DailyTrainTicketPO po = BeanUtil.copyProperties(dto, DailyTrainTicketPO.class);
        LambdaQueryWrapper<DailyTrainTicketPO> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ObjectUtil.isNotNull(po.getDate()), DailyTrainTicketPO::getDate, po.getDate())
                .eq(ObjectUtil.isNotNull(po.getTrainCode()), DailyTrainTicketPO::getTrainCode, po.getTrainCode())
                .eq(ObjectUtil.isNotNull(po.getStart()), DailyTrainTicketPO::getStart, po.getStart())
                .eq(ObjectUtil.isNotNull(po.getEnd()), DailyTrainTicketPO::getEnd, po.getEnd());
        return BeanUtil.copyProperties(this.selectOne(queryWrapper), DailyTrainTicketQueryOneResp.class);
    }

    boolean reduceDailyTrainTicket(@Param("trainCode") String trainCode,
                                   @Param("date") LocalDate date,
                                   @Param("seatType") String seatType,
                                   @Param("minStart") int minStart,
                                   @Param("maxStart") int maxStart,
                                   @Param("minEnd") int minEnd,
                                   @Param("maxEnd") int maxEnd);
}